//
//  WNBuriedManager.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/2.
//

import UIKit
import FBSDKCoreKit

class WNBuriedManager: NSObject {
    
    static let shared = WNBuriedManager()
    
    func startEvents(_ event: String, _ time: Double = 0.0) {
        provider_login.rx.request(.events(event, time))
            .asObservable().mapModel(BaseModel<Any>.self)
            .subscribe(onNext: { (model) in
            }, onError: { (error) in
            }).disposed(by: rx.disposeBag)
    }
    
    func readReport(_ bookId: String, time: Int) {
        provider_login.rx.request(.readReport(bookId, time))
            .asObservable().mapModel(BaseModel<Any>.self)
            .subscribe(onNext: { (model) in
            }, onError: { (error) in
            }).disposed(by: rx.disposeBag)
    }
    
    func active() { // 激活App
        AppEvents.shared.activateApp()
    }

    
    func enterPage(_ pageName: String) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("view"), parameters: [AppEvents.ParameterName("pageName"): pageName, AppEvents.ParameterName("type"): "进入"])
#endif
    }
    
    func leavePage(_ pageName: String) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("view"), parameters: [AppEvents.ParameterName("pageName"): pageName, AppEvents.ParameterName("type"): "离开"])
#endif
    }
    
    func signIn(_ method: String, _ userId: String) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("sign_in"), parameters: [AppEvents.ParameterName("userId"): userId])
        AppEvents.shared.logEvent(AppEvents.Name.completedRegistration, parameters: [AppEvents.ParameterName.registrationMethod: method])
#endif
    }
    
    func search(_ searchStr: String) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name.searched, parameters: [AppEvents.ParameterName.searchString: searchStr, AppEvents.ParameterName.contentType: "novel"])
#endif
    }
    
    func contact() {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name.contact)
#endif
    }
    
    func rated(_ ratestars: Double) { // 用户评分
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name.rated, valueToSum: ratestars, parameters: [AppEvents.ParameterName.maxRatingValue: 5])
#endif
    }
    
    func openBookDetailStart(_ bookId: String) {
#if !DEBUG
#endif
    }
    
    func readStart(_ bookId: String) {
#if !DEBUG
#endif
    }
    
    func choseChapterStart(_ bookId: String, _ chapterId: String) {
#if !DEBUG
#endif
    }
    
    func readChapterStart(_ bookId: String, _ chapterId: String) {
#if !DEBUG
#endif
    }
    
    func logProductItem(_ pid: String, _ title: String, _ price: Double) {
#if !DEBUG
        AppEvents.shared.logProductItem(id: pid, availability: AppEvents.ProductAvailability.inStock, condition: AppEvents.ProductCondition.new, description: "", imageLink: "", link: "", title: title, priceAmount: price, currency: "USD", gtin: nil, mpn: nil, brand: nil, parameters: nil)
#endif
    }
    
    func initiatedCheckout(_ price: Double) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("creat_order"), parameters: [AppEvents.ParameterName("amount"): price])
        AppEvents.shared.logEvent(AppEvents.Name.initiatedCheckout, valueToSum: price, parameters: [AppEvents.ParameterName.paymentInfoAvailable: 1, AppEvents.ParameterName.numItems: 1, AppEvents.ParameterName.currency: "USD"])
#endif
    }
    
    func addedPaymentInfo(_ price: Double) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name.addedPaymentInfo, valueToSum: price, parameters: [AppEvents.ParameterName.currency: "USD"])
#endif
    }

    func purchase(_ price: Double) {
#if !DEBUG
        AppEvents.shared.logPurchase(amount: recharge.price, currency: "USD")
#endif
    }
    
    func purchased(_ price: Double) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("verify_order"), parameters: [AppEvents.ParameterName("amount"): recharge.price])
        AppEvents.shared.logEvent(AppEvents.Name.purchased, valueToSum: price, parameters: [AppEvents.ParameterName.numItems: 1, AppEvents.ParameterName.currency: "USD"])
#endif
    }
    
    func buyBook(_ bookId: String) {
#if !DEBUG
#endif
    }
    
    func buyBookFailed(_ bookId: String) {
#if !DEBUG
#endif
    }
    
    func buyChapter(_ chapterId: String) {
#if !DEBUG
#endif
    }
    
    func buyChapterFailed(_ chapterId: String) {
#if !DEBUG
#endif
    }
    
    func checkIn(_ day: String) {
#if !DEBUG
        AppEvents.shared.logEvent(AppEvents.Name("checkIn"), parameters: [AppEvents.ParameterName("day"): day])
#endif
    }
}
